/******************************************************************************
 * RLT DSP
 * by Roberto Bender <beto.bender@gmail.com>
 * under GPL 2.0 license
 * 2009
******************************************************************************/

#include "filters.h"
#include "defines.h"
#include <stdio.h>
#define MAX_FILTERS_SIZE 20

struct FilterParams * m_FiltersP[MAX_FILTERS_SIZE];
FilterFunction m_FiltersF[MAX_FILTERS_SIZE];

int m_Filters = 0;
workingWord m_Amp = 1.0;
inputWord m_Silence = 0;
int m_Enabled = 1;

int StartFiltersEngine()
{
    return FILTER_SUCCESS;
}

int StopFiltersEngine()
{
    return FILTER_SUCCESS;
}

int AddFilter(struct FilterParams* params, FilterFunction function)
{
    if(m_Filters >= MAX_FILTERS_SIZE) 
        return FILTER_MAXED_OUT; 

    m_FiltersP[m_Filters] = params;
    m_FiltersF[m_Filters] = function;
    ++m_Filters;
    
    return FILTER_SUCCESS;
}

void ClearFilters()
{
    m_Filters = 0;
}

void EnableFilters()
{
    m_Enabled = 1;
}

void DisableFilters()
{
    m_Enabled = 0;
}

outputWord CalcFilter(inputWord input)
{
    int i = 0;
    outputWord output = input;
    
    if((outputWord)(output - m_Silence) > output) output = WORD_MIN;
    else output = output - m_Silence;

    for(; i < m_Filters && m_Enabled; ++i)
    {
        FilterFunction function = m_FiltersF[i];
        output = function(output, m_FiltersP[i]);
    }
    
    return output;
}
